<?php
/**
 * User: Hufeng
 * Date: 2017/08/29 19:48
 * Desc: 交易所Model
 */
namespace App\Model;
use SasPHP\SqlModel;

class StockModel extends SqlModel{
    protected $dbName = 'stock';

    public function __construct(){
        parent::__construct('DB_SERVER');
    }

    function insertData($data){
        $this->dbConn->insert($this->dbName,$data);
        return $this->dbConn->id();
    }

    function editData($where,$data){
        $this->dbConn->update($this->dbName,$data,$where);
    }

    function delData($data){
        $this->dbConn->delete($this->dbName, $data);
    }

    function findData($where,$fields='*'){
        $res = $this->dbConn->get($this->dbName,$fields,$where);
        return $res;
    }

    function queryData($where,$fields='*'){
        $res = $this->dbConn->select($this->dbName,$fields,$where);
        return $res;
    }
    //获取统计数据
    public function getTotalNum($where){
        $res = $this->dbConn->count($this->dbName,$where);
        return $res;
    }
    //sum数据
    public function sumData($where,$param){
        $res = $this->dbConn->sum($this->dbName,$param,$where);
        return $res;
    }

    //验证股票开市时间和价格
    public function stockTradeCheck($sid,$price){
        //验证开市时间
        $sRes = $this->findData(['id'=>$sid,'status'=>1]);
        if(!$sRes){
            return ['code'=>1,'msg'=>'该股票暂未开盘'];
        }
        $tpModel = new TradePlaceModel();
        $tpRes = $tpModel->findData(['id'=>$sRes['tpid'],'status'=>1]);
        if(!$tpRes){
            return ['code'=>1,'msg'=>'该股票暂未开盘'];
        }
        $week = date('w');//周几
        if($week==0){
            $week = 7;
        }
        $weeks = explode(',',$tpRes['week_list']);
        if(!in_array($week,$weeks)){
            return ['code'=>1,'msg'=>'该股票暂未开盘'];
        }
        $nowT = intval(date('H').date('i'));
        list($shour,$smin) = explode(':',$tpRes['stime']);
        $sT = intval($shour.$smin);
        list($ehour,$emin) = explode(':',$tpRes['etime']);
        $eT = intval($ehour.$emin);
        if($nowT<$sT || $nowT>$eT){
            return ['code'=>1,'msg'=>'该股票暂未开盘'];
        }
        //验证价格
        $shModel = new StockHandicapModel();
        $rangPrice = $shModel->rangPrice($sid,$tpRes['rang_space']);
        if($price>$rangPrice['maxPrice'] || $price<$rangPrice['minPrice']){
            return ['code'=>1,'msg'=>'委托价格溢出'];
        }
        return ['code'=>0,'msg'=>'OK','tradeFee'=>$tpRes['trade_fee']];
    }


}
